<?php
namespace Little\Controller;
header("Content-type: text/html; charset=utf-8");
use Think\Controller;

//********************S首页************************

class IndexController extends Controller
{
    //首页信息展示
    public function index(){
        //存储用户信息
        $code = I('code');
        if($code){$user = user_add($code,"id");}
        $list = $this->gong(1);
        echo json_encode($list);
    }
    //顶级分类切换
    public function qie(){
        $qie_id = I("qie_id");
        $info = $this->gong($qie_id);
        echo json_encode($info);
    }
    //精品推荐公用  $type_id mei_type表
    private function gong($type_id){
        //轮播图
        $lun = sels("news_lun","id,img,lun_id,status","type_id = {$type_id}");
        if(!$lun){
            $lun = 1;
        }else{
            foreach ($lun as $k=>$v){
                $lun[$k]['img'] = IMGS.$v['img'];
            }
        }
        if($type_id == 1){  //精品推荐
            //精品推荐医疗美容等分类
            $ding_type = sels("mei_type","id,name","pid = 0");
            foreach ($ding_type as $k=>$v){
                $ding_type[$k]['ding'] = 1;//页面判断用的参数
            }
            //标签分类
            $type = sels("ke_type","id,img,name");
            foreach ($type as $k=>$v){
                $type[$k]['img'] = IMGS.$v['img'];
                if($v['id'] == 3){
                    $type[$k]['tiao'] = 2; //页面判断用的参数
                }else{
                    $type[$k]['tiao'] = 1;
                }
            }
            //名师精品课
            $table = "goods_zhu";
            $filed = "a.id,a.img,a.title,b.name";
            $join = array("a left join __TEACHER__ b on a.teacher_id = b.id");
            $where = "a.tui = 2";
            $order = "a.id desc";
            $limit = "6";
            $ke = joins($table,$filed,$join,$where,$order,$limit);
            if(!$ke){
                $ke = 1;
            }else{
                foreach ($ke as $k=>$v){
                    $ke[$k]['img'] = IMGS.$v['img'];
                }
            }
            //推荐专家
            $teacher = sels("teacher","id,name,avatar as img","tui = 2","id desc","6");
            if(!$teacher){
                $teacher = 1;
            }else{
                foreach ($teacher as $k=>$v){
                    $count = zong_shu("collect_teacher","goods_id = {$v['id']}");
                    $teacher[$k]['guan'] = $count;
                    $teacher[$k]['img'] = IMGS.$v['img'];
                }
            }
            //课程每日精选
            $filed = "id,img,title,nums,moneys,status";
            $jing = sels("goods_zhu",$filed,"jing = 2","id desc","4");
            $jing = ke($jing);
            $list = array("lun" => $lun,"type"=>$type,"ding_type"=>$ding_type, "ke" => $ke, "teacher" => $teacher,"jing"=>$jing);
        }elseif ($type_id == 4){  //加盟合作
            $jia_type = sels("join_type","id,name","pid != 0");
            //默认显示第一种分类小的资讯
            if($jia_type){
                $news = sels("news","id,img","type_two_id = {$jia_type[0]['id']}");
                if(!$news){
                    $news = 1;
                }else{
                    foreach ($news as $k=>$v){
                        $news[$k]['img'] = IMGS.$v['img'];
                    }
                }
            }
            $list = array("lun" => $lun,"info"=>$news,"type"=>$jia_type);
        }else{
            //门店推荐  /院线推荐
            $tui = sels("news","id,img","lei_id = {$type_id}");
            if(!$tui){
                $tui = 1;
            }else{
                foreach ($tui as $k=>$v){
                    $tui[$k]['img'] = IMGS.$v['img'];
                }
            }
            //分类
            $er_type = sels("mei_type","id,name","pid = {$type_id}");//二级分类名
            if($er_type){
                foreach($er_type as $k=>$v){
                    //三级分类
                    $san_type           = sels("mei_type","id,name","pid = {$v['id']}");
                    if($san_type){
                        $er_type[$k]['er'] = $this->er($san_type);
                    }else{
                        $er_type[$k]['er'] = 1;
                    }
                }
            }
            $list = array("lun" => $lun,"tui"=>$tui,"list"=>$er_type);
        }
        return $list;
    }
    private function er($san_type){
        foreach ($san_type as $kk=>$vv){
            //查出最终分类下的第一种分类的课程
            $filed     = "id,img,title,nums,moneys,status";
            $list      = sels("goods_zhu",$filed,"type_three_id = {$vv['id']}","id desc","2");
            if(!$list){
                $list = 1;
            }else{
                $list = ke($list);
            }
            $san_type[$kk]['list'] = $list;
           /* while($x<=5) {
                echo "这个数字是：$x <br>";
                $x++;
            }*/
        }
        return $san_type;
    }
}